package com.kgc.smbms.controller;


import com.github.pagehelper.PageInfo;
import com.kgc.smbms.pojo.SmbmsBill;
import com.kgc.smbms.pojo.SmbmsUser;
import com.kgc.smbms.service.SmbmsBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/bill")
public class SmbmsBillController {

    @Autowired
    private SmbmsBillService smbmsBillService;

    @RequestMapping("/list")
    public String list(@RequestParam(required = false,defaultValue = "") String productname,
                       @RequestParam(required = false,defaultValue = "0") Long providerid,
                       @RequestParam(required = false,defaultValue = "0") Integer ispayment,
                       @RequestParam(required = false,defaultValue = "1") Integer pageNo,
                       Model model){

        PageInfo<SmbmsBill> pageInfo = smbmsBillService.selectBillByDondetion(productname, providerid, ispayment, pageNo,5);

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("productname",productname);
        model.addAttribute("providerid",providerid);
        model.addAttribute("ispayment",ispayment);


        //当数据查询出来之后，直接设置到数据显示页面
        return "billlist";
    }

    @RequestMapping("/delete")
    public String deleteBill(Long id){
        //返回值不用接收
        smbmsBillService.deleteBill(id);
        return "redirect:/bill/list";
    }

    @RequestMapping("/update")
    public String updateBill(SmbmsBill bill,HttpSession session,Model model){
        //获取用户对象
        SmbmsUser smbmsUser = (SmbmsUser)session.getAttribute("smbmsUser");

        //判断用户是否存在
        if(null == smbmsUser){
            model.addAttribute("error","请登录后再修改订单");
            return "forward:/login.jsp";//转发到登录页面
        }

        //设置修改者id
        bill.setModifyby(smbmsUser.getId());

        //设置修改时间
        bill.setModifydate(new Date());

        //调
        int row = smbmsBillService.updateBill(bill);

        //如果修改失败
        if(row == 0){
            return "redirect:/bill/toUpdate?id="+bill.getId();

        }

        //如果修改成功，就跳转到主页
        return "redirect:/bill/list";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Long id,Model model){
        //调
        SmbmsBill smbmsBill = smbmsBillService.selectBillById(id);
        //存
        model.addAttribute("bill",smbmsBill);
        //转
        return "billmodify";
    }

    @RequestMapping("/view")
    public String viewBill(Long id,Model model){
        //调
        SmbmsBill smbmsBill = smbmsBillService.selectBillById(id);
        //存
        model.addAttribute("bill",smbmsBill);

        //转，把结果查询到了之后需要中转到详情页面
        return "billview";
    }

    //添加信息的控制器
    @RequestMapping("/add")
    public String addBill(SmbmsBill bill, HttpSession session,Model model){
        SmbmsUser smbmsUser = (SmbmsUser)session.getAttribute("smbmsUser");
        //判断用户是否登录
        if(null == smbmsUser){
            model.addAttribute("error","请登录后再添加订单");
            return "forward:/login.jsp";
        }
        //设置创建者
        bill.setCreatedby(smbmsUser.getId());

        //设置创建时间
        bill.setCreationdate(new Date());
        //调用业务层的添加方法
        int i = smbmsBillService.addBill(bill);
        if(i == 0){
            return "redirect:/jsp/billadd.jsp";
        }

        return "redirect:/bill/list";
    }


}
